package com.skt.common_hr.hrletter.events
{
	import com.skt.common_hr.common.components.CommonAlert;
	import com.wcl.components.HanDateField;
	import com.wcl.utils.DateUtil;
	
	import flash.events.FocusEvent;
	
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.ValidationResultEvent;
	import mx.formatters.DateFormatter;
	import mx.validators.DateValidator;
	
	public class CustomDateField extends HanDateField  // DateField 상속
	{
		private var dateFormat:DateFormatter = new DateFormatter();  // DateFormatter 생성
		private var dateVali:DateValidator = new DateValidator();      // date의 값을 체크
		
		//private var currDate:String;
		private var startDate:String;
		
		public function CustomDateField()
		{
			this.addEventListener(CalendarLayoutChangeEvent.CHANGE,changed_data);
			super();
		}
		
		private function changed_data(event:CalendarLayoutChangeEvent):void{
			dateFormat.formatString = formatString;
			//dateFormat.formatString = "";
			this.text = dateFormat.format(this.text);
		}
		
		public function set chackDate(strDate:String):void
		{
			var yearEg:String = strDate.substr(0,4);
			var moEg:String   = strDate.substr(5,2);
			var dayEg:String  = strDate.substr(8,2);
			var dateEg:Date   = new Date(yearEg,int(moEg),int(dayEg));
			
			var chackdate:Date = new Date(dateEg);
			chackdate.month += -1;
			
			var changeStDate:String;
			changeStDate = DateUtil.toDefaultString(chackdate);
			this.startDate = changeStDate;
		}
		
		private function focus_Handler(event:FocusEvent):void{
			dateVali.source = textInput;
			dateVali.property = "text";
			dateVali.required = false;
			dateVali.allowedFormatChars = "-";
			dateVali.inputFormat = "YYYY-MM-DD";
			
			if(dateVali.validate().type == ValidationResultEvent.VALID){
				var str:String = textInput.text;
				if(str.length == 8 ){
					dateVali.inputFormat = formatString;
					this.text = str.substring(0,4)+"-"+str.substring(4,6)+"-"+str.substring(6,8);
				}
			}else{
				//this.text = textInput.text;
				this.text = startDate;
				CommonAlert.showMessage("Notice", "ALERT_INVALID_DATE");
			}
			textInput.errorString = "";
		}
		// textInput이 생성되었을때 이벤트 리스너를 추가 한다
		override protected function createChildren():void{ 
			super.createChildren();
			
			if(textInput)
				textInput.addEventListener(FocusEvent.FOCUS_OUT,focus_Handler);
			}
	}
}